package agenceVoyageVirtuel.client.control;

import agenceVoyageVirtuel.client.beans.ClientData;
import agenceVoyageVirtuel.client.rpc.RpcCallServiceAsync;
import agenceVoyageVirtuel.client.rpc.RpcInit;
import agenceVoyageVirtuel.client.viewer.PanierViewer;
import agenceVoyageVirtuel.shared.ErrorDialog;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.ScrollPanel;

public class EspacePersoPanier extends Composite {
	private final PanierViewer panierViewer;
	private final ConnexionPanel connexionPanel;
	private RpcCallServiceAsync rpc;
	protected HTML error;
	private final HorizontalPanel horizontalPanel;
	private final HeaderPanel headerPanel;

	public EspacePersoPanier(HeaderPanel headerPanel) {
		this.headerPanel = headerPanel;
		
		horizontalPanel = new HorizontalPanel();
		initWidget(horizontalPanel);
		
		connexionPanel = new ConnexionPanel(this);
		horizontalPanel.add(connexionPanel);
		
		panierViewer = new PanierViewer();
		horizontalPanel.add(panierViewer);
		
		rpc = RpcInit.initRpc();
	}

	public PanierViewer getPanierViewer() {
		return panierViewer;
	}

	public ConnexionPanel getConnexionPanel() {
		return connexionPanel;
	}

	public void login(String email, String password) {
		rpc.isAuthentifie(email, password, new AsyncCallback<ClientData>() {
			
			@Override
			public void onSuccess(ClientData result) {
				if(result == null)
					ErrorDialog.standardErrorDialog(new Label("Votre nom d'utilisateur ou votre mot de passe sont incorrects")).show();
				login(result);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				ErrorDialog.standardErrorDialog(new Label("Une erreur s'est produite lors de votre connection")).show();
			}
		});
	}

	protected void login(ClientData result) {
		horizontalPanel.clear();
		headerPanel.login(result);
		if(result == null){
			connexionPanel.clearPass();
			horizontalPanel.add(connexionPanel);
			horizontalPanel.add(panierViewer);
			return;
		} else {
			VerticalPanel verticalPanel = new VerticalPanel();
			verticalPanel.add(new Label("Bienvenue, Monsieur "+result.getAdresseClient().getPersonne().getPrenom()+" "+result.getAdresseClient().getPersonne().getNom()));
			Button button = new Button("Deconnexion");
			button.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent event) {
					login(null);
				}
			});
			verticalPanel.add(button);
			horizontalPanel.add(verticalPanel);
			horizontalPanel.add(panierViewer);
		}
	}

}
